package com.example.timepicker;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.example.data.DBManger;
import com.example.timecalculous.MainActivity;
import com.example.timecalculous.R;
import com.example.data.TimeData;
import com.example.timepicker.PickerView.onSelectListener;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class ItemDetails extends Activity {
    PickerView start_hour_pv,start_minute_pv;
    PickerView end_hour_pv,end_minute_pv;
    public TimeData timedata;
    DBManger dbManger;
    int TIME_ID;
    AlertDialog dialog,dialog1;
    String start_hour="12",start_minute="30",end_hour="12",end_minute="30",itemdetails;
    String m,n;
    int year,month,day,hour,minute,second;
    String year1=null,month1=null,day1=null;
    Calendar calendar=Calendar.getInstance();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        final EditText etxt;
        final Button btn;
        final TextView textView1,textView2;
        ImageButton imageButton;
        ToggleButton toggleButton,toggleButton1;
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.itemdetails);
        Bundle bundle=this.getIntent().getExtras();
        TIME_ID=bundle.getInt("timeid");
        timedata=new TimeData(this);
        //设置当前时间
        calendar.setTimeInMillis(System.currentTimeMillis());
        year=calendar.get(Calendar.YEAR);
        month=calendar.get(Calendar.MONTH);
        day=calendar.get(Calendar.DAY_OF_MONTH);
        hour=calendar.get(Calendar.HOUR_OF_DAY);
        minute=calendar.get(Calendar.MINUTE);
        second=calendar.get(Calendar.SECOND);
        year1=String.valueOf(year);
        month1=String.valueOf(month);
        day1=String.valueOf(day);
		/*
		 * 开始时间设置
		 */
        start_hour_pv = (PickerView) findViewById(R.id.start_hour_pv);
        List<String> data = new ArrayList<String>();
        for (int i = 0; i < 24; i++)
        {
                data.add(i < 10 ?  "0"+i : "" + i);
        }
        start_hour_pv.setData(data);
        start_hour_pv.setOnSelectListener(new onSelectListener() {

            @Override
            public void onSelect(String text1) {
                // TODO Auto-generated method stub
                Toast.makeText(ItemDetails.this, "选择了 " + text1 + " 时",
                        Toast.LENGTH_SHORT).show();
                start_hour= text1;
            }
        });
        start_minute_pv = (PickerView) findViewById(R.id.start_minute_pv);
        List<String> seconds = new ArrayList<String>();
        for (int i = 0; i < 60; i++)
        {
            seconds.add(i < 10 ?  "0"+i : "" + i);
        }
        start_minute_pv.setData(seconds);
        start_minute_pv.setOnSelectListener(new onSelectListener() {

            @Override
            public void onSelect(String text2) {
                // TODO Auto-generated method stub
                Toast.makeText(ItemDetails.this, "选择了 " + text2 + " 分",
                        Toast.LENGTH_SHORT).show();
                start_minute=text2;
            }
        });
		 /*
		  * 终止时间设置
		  */
        end_hour_pv = (PickerView) findViewById(R.id.end_hour_pv);
        List<String> data1 = new ArrayList<String>();
        for (int i = 0; i < 24; i++)
        {
            data1.add(i < 10 ?  "0"+i : "" + i);
        }
        end_hour_pv.setData(data);
        end_hour_pv.setOnSelectListener(new onSelectListener() {

            @Override
            public void onSelect(String text1) {
                // TODO Auto-generated method stub
                Toast.makeText(ItemDetails.this, "选择了 " + text1 + " 时",
                        Toast.LENGTH_SHORT).show();
                end_hour=text1;
            }
        });
        end_minute_pv = (PickerView) findViewById(R.id.end_minute_pv);
        List<String> seconds1 = new ArrayList<String>();
        for (int i = 0; i < 60; i++)
        {
            seconds1.add(i < 10 ? "0"+i : "" + i);
        }
        end_minute_pv.setData(seconds);
        end_minute_pv.setOnSelectListener(new onSelectListener() {

            @Override
            public void onSelect(String text2) {
                // TODO Auto-generated method stub
                Toast.makeText(ItemDetails.this, "选择了 " + text2 + " 分",
                        Toast.LENGTH_SHORT).show();
                end_minute=text2;
            }
        });
        etxt=(EditText) findViewById(R.id.itemSheZhi);
        btn=(Button) findViewById(R.id.btn_rsv);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                itemdetails=etxt.getText().toString();
                update(TIME_ID, year1,month1,day1,start_hour, start_minute, end_hour, end_minute,itemdetails);
                timedata.close();
                Intent intent=new Intent(ItemDetails.this,MainActivity.class);
                startActivity(intent);
                ItemDetails.this.finish();
            }
        });
        imageButton= (ImageButton) findViewById(R.id.btn_rtn);
        imageButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(ItemDetails.this,MainActivity.class);
                startActivity(intent);
                ItemDetails.this.finish();
            }
        });
        textView1= (TextView) findViewById(R.id.txt_stl);
        textView2= (TextView) findViewById(R.id.txt_msc);
        /*
      以对话框形的式设置“提醒方式”
         */
        textView1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[]data={"响铃","响铃及震动","震动"};
                AlertDialog.Builder builder=new AlertDialog.Builder(ItemDetails.this);
                builder.setItems(data, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String select=data[which];
                        textView1.setText("提醒方式："+select);
                        m=select;
                    }
                });
                dialog=builder.create();
                dialog.show();
            }
        });
        textView2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                final String []data1={"莺啼序","雪见落入凡尘","追"};
                AlertDialog.Builder builder=new AlertDialog.Builder(ItemDetails.this);
                builder.setItems(data1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String select=data1[which];
                        textView2.setText("铃声："+select);
                        n=select;
                    }
                });
                dialog1=builder.create();
                dialog1.show();
            }
        });
        /*
        获取系统时间
         */

        toggleButton= (ToggleButton) findViewById(R.id.togButtonStart);
        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int StartHour=Integer.valueOf(start_hour).intValue();
                int StartMinute=Integer.valueOf(start_minute).intValue();
                if (isChecked) {
                    calendar.set(year,month,day,StartHour,StartMinute,0);
                    Intent intent=new Intent(ItemDetails.this, AlarmReceiver.class);
                    PendingIntent pi=PendingIntent.getBroadcast(ItemDetails.this,0,intent,0);
                    AlarmManager am= (AlarmManager) getSystemService(ALARM_SERVICE);
                    am.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pi);
                }
            }
        });
        toggleButton1= (ToggleButton) findViewById(R.id.togButtonEnd);
        toggleButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int EndHour=Integer.valueOf(end_hour).intValue();
                int EndMinute=Integer.valueOf(end_minute).intValue();
                if (isChecked) {
                    calendar.set(year,month,day,EndHour,EndMinute,0);
                    Intent intent=new Intent(ItemDetails.this, AlarmReceiver.class);
                    PendingIntent pi=PendingIntent.getBroadcast(ItemDetails.this,0,intent,0);
                    AlarmManager am= (AlarmManager) getSystemService(ALARM_SERVICE);
                    am.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pi);
                    }
                }
        });
    }
    //修改操作
    public void update(int id, String year,String month,String day,String start_hour,String start_minut,String end_hour,
                       String end_minut,String details)
    {
        dbManger=new DBManger();

        SQLiteDatabase db = timedata.getWritableDatabase();
        String where = dbManger.TIME_ID + " = ?";
        String[] whereValue = { Integer.toString(id) };

        ContentValues cv = new ContentValues();
        cv.put(dbManger.YEAR, year);
        cv.put(dbManger.MONTH, month);
        cv.put(dbManger.DAY, day);
        cv.put(dbManger.START_TIME_HOUR, start_hour);
        cv.put(dbManger.START_TIME_MINUT, start_minut);
        cv.put(dbManger.END_TIME_HOUR, end_hour);
        cv.put(dbManger.END_TIME_MINUT, end_minut);
        cv.put(dbManger.ITEM_EDTAILS, details);
        db.update(dbManger.TABLE_NAME, cv, where, whereValue);
    }
    }
